package com.pengtu.gsj.dao.check;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.hibernate.type.StandardBasicTypes;
import org.springframework.stereotype.Repository;

import com.pengtu.gsj.dao.hibernate.HibernateDao;
import com.pengtu.gsj.entity.CheckResultVo;
import com.pengtu.gsj.entity.check.CheckModule;

@Repository
public class CheckModuleDao extends HibernateDao<CheckModule, String> {

	@SuppressWarnings("unchecked")
	public List<CheckModule> getCheckModule(String projectId){
		String sql = "SELECT t.* from T_CK_CHECKMODULE t, R_APP_CHECKMENU s where t.projectId =? and t.moduleId = s.MENUID and t.projectId = s.CHECKID order by t.sort ASC";
		Query query = getSession().createSQLQuery(sql).addEntity(CheckModule.class);
		query.setString(0, projectId);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<CheckResultVo> getCheckResultVoByParentId(String projectId){
		String sql = "SELECT sum(t.tableFull) as tableFull, sum(t.testFull) as testFull, sum(t.testFull)/sum(t.tableFull) as perfection from T_CK_CHECKMODULE t, R_APP_CHECKMENU s where t.projectId =? and t.moduleId = s.MENUID and t.projectId = s.CHECKID;";
		Query query = getSession().createSQLQuery(sql)
				.addScalar("tableFull", StandardBasicTypes.DOUBLE)
				.addScalar("testFull", StandardBasicTypes.DOUBLE)
				.addScalar("perfection", StandardBasicTypes.DOUBLE)
				.setResultTransformer(Transformers.aliasToBean(CheckResultVo.class));
		query.setString(0, projectId);
		return query.list();
	}
}
